<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Register & Login</title>
    <style>
      body {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        gap: 20px;
        margin: 0;
        font-family: Arial, sans-serif;
        background-color: #f9f9f9;
      }

      .page {
        display: flex;
        justify-content: center;
        align-items: center;
        text-align: center;
        width: 300px;
        height: 300px;
        border: 2px solid #333;
        background-color: #fff;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
      }

      form {
        display: flex;
        flex-direction: column;
        gap: 10px;
      }

      input {
        padding: 5px;
        width: 200px;
      }

      button {
        padding: 5px;
      }

      a {
        color: #007bff;
        text-decoration: none;
        cursor: pointer;
      }

      a:hover {
        text-decoration: underline;
      }

      #login {
        display: none;
      }
    </style>
  </head>

  <body>
    <div id="register" class="page">
      <form>
        <input type="text" id="reg-username" placeholder="Username" />
        <input type="password" id="reg-password" placeholder="Password" />
        <button type="button">Register</button>
        <a href="#" name="login">Go to Login</a>
      </form>
    </div>

    <div id="login" class="page">
      <form>
        <input type="text" id="login-username" placeholder="Username" />
        <input type="password" id="login-password" placeholder="Password" />
        <button type="button">Login</button>
        <a href="#" name="register">Go to Register</a>
      </form>
    </div>
    <!-- 自定义提示框 -->
    <div
      id="toast"
      style="
        display: none;
        position: fixed;
        top: 20px;
        left: 50%;
        transform: translateX(-50%);
        padding: 10px 20px;
        background: #333;
        color: white;
        border-radius: 4px;
        z-index: 9999;
      "
    ></div>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
      let timer;
      //设置提示
      const toast = document.querySelector("#toast");
      function showToast(message) {
        toast.style.display = "block";
        toast.textContent = message;
        timer = setTimeout(() => {
          toast.style.display = "none";
        }, 2000);
      }

      // 切换显示/隐藏页面
      const registerPage = document.querySelector("#register");
      const loginPage = document.querySelector("#login");
      //点击超链接跳转到登录页面
      document
        .querySelector("a[name='login']")
        .addEventListener("click", (e) => {
          e.preventDefault();
          registerPage.style.display = "none";
          loginPage.style.display = "flex";
          if (timer) {
            clearTimeout(timer);
            timer = null;
          }
        });
      // 点击超链接跳转到注册页面
      document
        .querySelector("a[name='register']")
        .addEventListener("click", (e) => {
          e.preventDefault();
          loginPage.style.display = "none";
          registerPage.style.display = "flex";
          if (timer) {
            clearTimeout(timer);
            timer = null;
          }
        });
      // 注册
      document
        .querySelector("#register button")
        .addEventListener("click", (e) => {
          const username = document.querySelector("#reg-username").value.trim();
          const password = document.querySelector("#reg-password").value.trim();

          // 验证输入
          if (!username || !password) {
            alert("请输入用户名和密码");
            document.querySelector("#reg-username").value = "";
            document.querySelector("#reg-password").value = "";
            return;
          }
          // 注册请求
          axios
            .post("http://305356f7.r28.cpolar.top/users/register", {
              username,
              password,
            })
            .then((res) => {
              // alert("注册成功，三秒后自动跳转登录跳转登录！");
              showToast("注册成功，三秒后自动跳转登录");
              timer = setTimeout(() => {
                registerPage.style.display = "none";
                loginPage.style.display = "flex";
              }, 3000);
              console.log(res.data);
            })
            .catch((err) => {
              showToast("注册失败,请重新注册");
              document.querySelector("#reg-username").value = "";
              document.querySelector("#reg-password").value = "";
            });
        });
      //登录
      document.querySelector("#login button").addEventListener("click", (e) => {
        const username = document.querySelector("#login-username").value.trim();
        const password = document.querySelector("#login-password").value.trim();
        //验证输入
        if (!username || !password) {
          alert("请输入用户名和密码");
          document.querySelector("#login-username").value = "";
          document.querySelector("#login-password").value = "";
          return;
        }
        //登录请求
        axios
          .post("http://305356f7.r28.cpolar.top/users/login", {
            username,
            password,
          })
          .then((res) => {
            console.log(res.data);
            showToast("登录成功，三秒后自动跳转首页");
            const Username = res.data.username;
            localStorage.setItem("user", Username);
            setTimeout(() => {
              document.querySelector("#login-username").value = "";
              document.querySelector("#login-password").value = "";
              window.location.href = "./index.html";
            }, 3000);
          })
          .catch((err) => {
            showToast("登录失败，请重新登录");
            document.querySelector("#login-username").value = "";
            document.querySelector("#login-password").value = "";
          });
      });
    </script>
  </body>
</html>
